package com.hunanwenli.tool;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Getter
@AllArgsConstructor
public enum FileTypeEnum {
    /*
    文件类型枚举
     */
    FILE_TE_TXT("FILE_TE_",".txt"),
    FILE_HT_HTML("FILE_HT_",".html"),
    FILE_XM_XML("FILE_XM_",".xml"),

    FILE_EX_XLSX("FILE_EX_",".xlsx"),
    FILE_EX_XLS("FILE_EX_",".xls"),

    FILE_W_DOC("FILE_W_",".doc"),
    FILE_W_DOCX("FILE_W_",".docx"),

    FILE_P_PPT("FILE_P_",".ppt"),
    FILE_P_PPTX("FILE_P_",".pptx "),

    FILE_TU_JPG("FILE_TU_",".jpg"),
    FILE_TU_PNG("FILE_TU_",".png"),
    FILE_TU_PDF("FILE_TU_",".pdf"),
    FILE_TU_TIFF("FILE_TU_",".tiff"),
    FILE_TU_SWF("FILE_TU_",".swf"),

    FILE_SP_FLV("FILE_SP_",".flv"),
    FILE_SP_RMVB("FILE_SP_",".rmvb"),
    FILE_SP_MP4("FILE_SP_",".mp4"),
    FILE_SP_MVB("FILE_SP_",".mvb"),

    FILE_SY_WMA("FILE_SY_",".wma"),
    FILE_SY_MP3("FILE_SY_",".mp3");

    private final String suffix;

    private final String type;


    /**
     * @param fileName
     * @param fileType 看FileTypeEnum(文件类型枚举)des
     * @descriptiony 验证文件类型（简单验证文件后缀）
     */
    public static String checkFileType(String fileName,String fileType){
        if (StringUtils.isEmpty(fileName)){
            throw new RuntimeException("文件名称不能为空");
        }
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        if(!initEnumMap(fileType).containsKey(suffixName)){
            throw new RuntimeException("文件格式不对");
        }
        return suffixName;
    }

    /**
     * @param prefix 前缀
     * @return List<FileTypeEnum>
     * @description 返回List
     */
    public static List<FileTypeEnum> initEnumList(String prefix) {
        if (StringUtils.isEmpty(prefix)) {
            return new ArrayList<>();
        }
        return Arrays.stream(FileTypeEnum.values()).filter(e -> e.toString().startsWith(prefix)).collect(Collectors.toList());
    }

    /**
     * 返回map
     * @param prefix 前缀
     * @return Map<String, String>
     */
    public static Map<String, String> initEnumMap(String prefix) {
        return initEnumList(prefix).stream().collect(Collectors.toMap(FileTypeEnum::getType, FileTypeEnum::getType));
    }
}
